/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/
var bcrypt= require('bcrypt-nodejs');
module.exports = {

	//schema: true,

  attributes: {

	name: {
  		type: 'string',
  		required: true
  	},
  	username: {
  		type: 'string',
  		required: true,
  		unique: true
  	},
  	email: {
  		type: 'email',
  		required: true
  	},

    admin: {
      type: 'boolean',
      defaultsTo: true
    },

  	encryptedPassword: {
  		type: 'string'
  	}
  	
/*
  	,
  	toJSON: function () {
  		var obj=this.toObject();
  		delete obj.password;
  		delete obj.confirmation;
  		delete obj.encryptedPassword;
  		delete obj._csrf;
  		return obj;
  	}
  */
  	
  	},
        beforeCreate: function (values,next){
      if (!values.password || values.password != values.confirmation) {
        return next({err: ["Password dosn´t match password confirmation"]});
      };

      bcrypt.hash(values.password,null,null,function passwordEncrypted(err,encryptedPassword){
        if (err) return next(err);
        values.encryptedPassword=encryptedPassword;
        next();
      });


  }
};

